﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUL;

namespace EF04_Example
{
    public partial class AdvertiseManagement : Form
    {
        public AdvertiseManagement()
        {
            InitializeComponent();
        }
        Advertise_Business bul = new Advertise_Business();
        private int _id;
        private void Form1_Load(object sender, EventArgs e)
        {
            Text = ClsConstant.Title;

            bul.GetAllAdvertise(dgvAdvertise);
            btnReset_Click(sender,e);
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            //Validdate Data
           var validate = bul.ValidationData(txtName.Text, txtPossition.Text, txtOrder.Text);
           switch (validate)
           {
               case "N":
                   lbThongBao.Text = @"'Tên quảng cáo' không được bỏ trống!!";
                   return;
               case "P":
                   lbThongBao.Text = @"'Vị trí' không đúng!!";
                   return;
               case "O":
                   lbThongBao.Text = @"'Sắp xếp' không đúng!!";
                   return;
           }

            bul.InsertAdvertise(txtName.Text,txtLink.Text,txtPossition.Text,txtOrder.Text,txtContent.Text);

            bul.GetAllAdvertise(dgvAdvertise);
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            //Validdate Data
            var validate = bul.ValidationData(txtName.Text, txtPossition.Text, txtOrder.Text);
            switch (validate)
            {
                case "N":
                    lbThongBao.Text = @"'Tên quảng cáo' không được bỏ trống!!";
                    return;
                case "P":
                    lbThongBao.Text = @"'Vị trí' không đúng!!";
                    return;
                case "O":
                    lbThongBao.Text = @"'Sắp xếp' không đúng!!";
                    return;
            }

            bul.UpdateAdvertise(_id, txtName.Text, txtLink.Text, txtPossition.Text, txtOrder.Text, txtContent.Text);

            bul.GetAllAdvertise(dgvAdvertise);
      
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            bul.DeleteAdvertise(_id);
            bul.GetAllAdvertise(dgvAdvertise);
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            foreach (var control in groupBox1.Controls.OfType<TextBox>())
            {
                control.Text = "";
            }

            ChangeModeToAddNew(true);
            txtName.Focus();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            var newsManagement = new NewsManagement();
            newsManagement.ShowDialog();
        }


        private void DgvAdvertiseCellClick(object sender, DataGridViewCellEventArgs e)
        {
           var indexRowSelected = e.RowIndex;
            if (indexRowSelected <= -1) return;

            ChangeModeToAddNew(false);

            _id = Convert.ToInt32(dgvAdvertise[1, indexRowSelected].Value);

            txtName.Text = dgvAdvertise[2, indexRowSelected].Value.ToString();
            txtLink.Text = dgvAdvertise[3, indexRowSelected].Value.ToString();
            txtPossition.Text = dgvAdvertise[4, indexRowSelected].Value.ToString();
            txtOrder.Text = dgvAdvertise[5, indexRowSelected].Value.ToString();
            txtContent.Text = dgvAdvertise[6, indexRowSelected].Value.ToString();
        }

        private void ChangeModeToAddNew(bool isAddNew)
        {
            btnThemMoi.Enabled = isAddNew;
            btnSua.Enabled = !isAddNew;
            btnXoa.Enabled = !isAddNew;
        }
    }
}
